package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.mapper.SystemDictionaryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.shiro.MyRealm;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {
    @Autowired
    private SystemDictionaryMapper systemdictionaryMapper;
    @Autowired
    private MyRealm myRealm;


    @Override
    public void saveOrUpdate(SystemDictionary entity) {
        if(entity.getId()!=null)
        {
            systemdictionaryMapper.updateByPrimaryKey(entity);
            //更新完成后清除缓存
            myRealm.clearCached();
        }else {
            systemdictionaryMapper.insert(entity);
        }
    }

    @Override
    public void delete(Long id) {
        systemdictionaryMapper.deleteByPrimaryKey(id);
        //删除后清除缓存
        myRealm.clearCached();
    }

    @Override
    public SystemDictionary get(Long id) {
        return systemdictionaryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionary> list() {
        return systemdictionaryMapper.selectAll();
    }

    @Override
    public PageInfo<SystemDictionary> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<SystemDictionary> list = systemdictionaryMapper.selectForList(qo);
        return new PageInfo<>(list);
    }
}
